<template>
  <ych-modal
    :fullscreen="true"
    :title="title"
    :visible="visible"
    :confirmLoading="spinning > 0"
    :destroyOnClose="true"
    @cancel="handleCancel">
    <template slot="footer">
      <a-button key="close" @click="handleCancel">{{ $t('m.common.button.close') }}</a-button>
      <a-button v-show="currentTab > 0" type="default" @click="$refs['step'+currentTab].prevStep()">{{ $t('m.codegen.button.previous_step') }}</a-button>
      <a-button v-show="currentTab < 2" type="primary" @click="$refs['step'+currentTab].nextStep()">{{ $t('m.codegen.button.next_step') }}</a-button>
      <a-button type="primary" @click="$refs['step'+currentTab].save()">{{ $t('m.common.button.save') }}</a-button>
      <a-button v-show="currentTab == 2" type="primary" @click="$refs['step'+currentTab].finish()">{{ $t('m.codegen.button.genCode') }}</a-button>
    </template>
    <a-spin :spinning="spinning > 0">
      <a-steps :current="currentTab">
        <a-step :title="$t('m.codegen.label.data_model_selection')"/>
        <a-step :title="$t('m.codegen.label.form_page_config')"/>
        <a-step :title="$t('m.codegen.label.infor_config_code_gen')"/>
      </a-steps>
      <!--<a-divider/>-->
      <div style="height: 100%;">
        <Step0 v-show="currentTab === 0" :genConfigId="genConfigId" :objCode="objCode" ref="step0" :iProps="iProps"  @setProp="setProp"
               @prevStep="prevStep" @nextStep="nextStep" @finish="finish" @setGenConfigId="setGenConfigId"/>
        <Step1 v-show="currentTab === 1" :genConfigId="genConfigId" ref="step1" :iProps="iProps" @setProp="setProp"
               @prevStep="prevStep" @nextStep="nextStep" @finish="finish"/>
        <Step2 v-show="currentTab === 2" :genConfigId="genConfigId" ref="step2" :iProps="iProps" @setProp="setProp"
               @prevStep="prevStep" @nextStep="nextStep" @finish="finish"/>
      </div>
    </a-spin>
  </ych-modal>
</template>

<script>

  import Step0 from './codegenConfig/Step0'
  import Step1 from './codegenConfig/Step1'
  import Step2 from './codegenConfig/Step2'
  import { downloadFile } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'CodegenConfigDialog',
    components: {
      Step0,
      Step1,
      Step2,
    },
    data () {
      return {
        title: this.$t('m.codegen.button.genCode'),
        visible: false,
        genConfigId:"",
        objCode:"",
        spinning: 0,
        currentTab: 0,
        url: {
          genCode: YchConstant.PREFIX_EFORM + '/generator/codeGeneratorController/genCode',
        },
        iProps: {},
      }
    },
    methods: {
      setProp (key, value) {
        this.iProps[key] = value
      },
      handleCancel () {
        this.visible = false
        this.init()
      },
      nextStep () {
        this.currentTab += 1
        this.$refs['step' + this.currentTab].show()
      },
      prevStep () {
        this.currentTab -= 1
      },
      finish () {
        this.spinning++
        let sysGenConfig = this.iProps.sysGenConfig
        sysGenConfig.subTableList = null
        sysGenConfig.mainTable = null
        sysGenConfig.actAppObject = null
        downloadFile(this.url.genCode, this.iProps.sysGenConfig.moduleName + '.zip', sysGenConfig, 'POST').finally(() => {
          this.spinning--
          this.$emit('refreshParent')
          this.handleCancel()
        })
      },
      refreshParent(){

      },
      setGenConfigId(genConfigId){
        this.genConfigId = genConfigId
      },
      add(objCode){
        this.init()
        this.objCode = objCode
        this.visible = true
      },
      init(){
        this.genConfigId = ""
        this.iProps = {}
        this.currentTab = 0
      },
      edit(record){
        this.init()
        this.visible = true
        this.genConfigId = record.sysGenConfigId

      },
    },
  }
</script>
